package com.example.demo.loginmanage.controller;

import com.example.demo.loginmanage.service.LoginService;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Transactional
@CrossOrigin
public class LoginController {

    private Logger log = Logger.getLogger(LoginController.class);

    @Autowired
    private LoginService loginService;

    @RequestMapping(value="/login",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> goIndex(@RequestBody Map<String,Object> param, HttpServletRequest request) throws SQLException {
        Map<String,Object> statusMap = loginService.queryUserInfosByUsername(param,request);
        return statusMap;
    }

    /**
         * @Description 初始化页面，查询所有中心店面
         * @Author zhangxue
         * @Date 8:53 2018/6/3
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/initPage",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String initPage(@RequestBody Map<String,Object> param){
        JSONObject js= new JSONObject();
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(param.size()>=1){
            List<Map<String,Object>> resultList = loginService.queryAllSaleStore(param);
            resultMap.put("data",resultList);
            resultMap.put("status","1");
        }else{

            resultMap.put("status","0");
        }
        js = JSONObject.fromObject(resultMap);

        return js.toString();
    }

    /**
     * @Description 初始化页面，查询所有限行信息
     * @Author zhangxue
     * @Date 8:53 2018/6/3
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/initPaiPage",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String initPaiPage(@RequestBody Map<String,Object> param){
        JSONObject js= new JSONObject();
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(param.size()>=1){
            List<Map<String,Object>> resultList = loginService.queryAllTrafficInfo(param);
            resultMap.put("data",resultList);
            resultMap.put("status","1");
        }else{

            resultMap.put("status","0");
        }
        js = JSONObject.fromObject(resultMap);

        return js.toString();
    }

    /**
         * @Description 通过员工编号查询手机号
         * @Author zhangxue
         * @Date 10:34 2018/8/31
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/queryPhoneByUserNo",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryPhoneByUrl(@RequestBody Map<String,Object> param){
        JSONObject js= new JSONObject();
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap = loginService.queryPhoneByUserNo(param);
        js = JSONObject.fromObject(resultMap);

        return js.toString();
    }

}
